package com.example.github.io.stream.byteArray;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ByteArrayOutputStream 将数据写入多个文件
 */
public class ByteArrayOutputStreamExample {
    public static void main(String[] args) throws IOException {
        /*
        1. 先创建FileOutputStream 实例对象（需要写入的对象）
        2. 构建ByteArrayOutputStream 实例
        3. ByteArrayOutputStream 实例中写入数据
        4. 将写入数据的ByteArrayOutputStream 实例写到需要写入的对象中
         */
        FileOutputStream fis1 = new FileOutputStream("D:/logs/test1.txt");
        FileOutputStream fis2 = new FileOutputStream("D:/logs/test2.txt");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        // stream.write(65); // 写入字符 A

        // 写入字符串
        String s = "this is ByteArrayOutputStreamExample's test words!";
        byte[] bytes = s.getBytes();
        stream.write(bytes);

        stream.writeTo(fis1);
        stream.writeTo(fis2);
        stream.flush();
        stream.close();
        System.out.println("ByteArrayOutputStreamExample is success! ");
    }
}
